#include <iostream>

#include <SDL/SDL.h>

using namespace std;

#undef main

#define SDL_SUBSYSTEMS SDL_INIT_VIDEO

#define WIN_FLAGS SDL_ANYFORMAT|SDL_RESIZABLE
#define WIN_WIDTH 1024
#define WIN_HEIGHT 480
#define WIN_BPP 0

#define WIN_TITLE "Ukazka 'jednoduche' inicializace SDL grafiky"


/*
 * Globalni promenne
 */

SDL_Surface *g_screen;


/*
 * Inicializacni funkce
 */

bool Init()
{
    // Inicializace SDL
    if(SDL_Init(SDL_SUBSYSTEMS) == -1)
    {
        fprintf(stderr, "Unable to initialize SDL: %s\n",
                SDL_GetError());
        return false;
    }

    // Vytvori okno s definovanymi vlastnostmi
    g_screen = SDL_SetVideoMode(WIN_WIDTH, WIN_HEIGHT, WIN_BPP, WIN_FLAGS);

    if(g_screen == NULL)
    {
        fprintf(stderr, "Unable to set %dx%d video: %s\n",
                WIN_WIDTH, WIN_HEIGHT, SDL_GetError());
        return false;
    }

    // Titulek okna
    SDL_WM_SetCaption(WIN_TITLE, NULL);

    return true;
}


/*
 * Deinicializacni funkce
 */

void Destroy()
{
    SDL_Quit();
}


/*
 * Osetruje udalosti (bude probrano nekdy v budoucnu)
 * V tomto pripade ceka se na klavesu ESC, ktera ukonci program
 */

bool ProcessEvent()
{
    SDL_Event event;

    while(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                    case SDLK_ESCAPE:
                        return false;
                        break;

                    default:
                        break;
                }
                break;

            case SDL_QUIT:
                return false;
                break;

            default:
                break;
        }
    }

    return true;
}


/*
 * Vstup do programu
 */

int main(int argc, char *argv[])
{
    printf(WIN_TITLE);
    printf("\nPress ESC key to quit.\n");

    // Inicializace
    if(!Init())
    {
        Destroy();
        return 1;
    }

    // Hlavni smycka programu
    bool done = false;
    while(!done)
    {
        done = !ProcessEvent();
        // TODO: Tady by se vykreslovalo
    }

    // Deinicializace a konec
    Destroy();
    return 0;
}

